package com.rainbow.controller;

import com.alibaba.fastjson.JSON;
import com.rainbow.service.ProductService;
import com.rainbow.domain.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;
    // 商品信息查询
    @RequestMapping("/product/{pid}")
    public Product product(@PathVariable("pid") Integer pid){
        log.info("{}号商品查询",pid);
        Product product  = productService.findByPid(pid);
        log.info("商品信息 {}", JSON.toJSONString(product));

        return product;
    }
    // 商品减库存
    @PostMapping("/product/sale")
    public String sale(@RequestParam("pid") Integer pid
            ,@RequestParam("number") Integer number
    )  {
        productService.minusStock(pid,number);
        return "sale success";
    }


    @RequestMapping("/test")
    public String teat(){
        log.info("test ");
        return "test";
    }

    @RequestMapping("/product/api1/demo1")
    public String demo1(){
        return "demo1";
    }
    @RequestMapping("/product/api1/demo2")
    public String demo2(){
        return "demo2";
    }
    @RequestMapping("/product/api2/demo1")
    public String demo3(){
        return "demo3";
    }
    @RequestMapping("/product/api2/demo2")
    public String demo4(){
        return "demo4";
    }
}
